package edu.pku.sei.transformation.editor.actions;

import org.eclipse.jface.action.Action;

import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.transformation.editor.model.ModelFactory;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.goal.Description;
import edu.pku.sei.transformation.editor.model.goal.TransformationGoal;
import edu.pku.sei.transformation.editor.views.dialogs.GoalDescriptionDialog;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class CreateDescriptionAction extends Action {
	private TransformationGoal goal;

	CreateDescriptionAction(TransformationGoal goal) {
		this.setText("Add Description");
		this.goal = goal;
		this.setImageDescriptor(IconsProvider
				.getImageDescriptor(IconsProvider.ADD_ICO));
	}

	@Override
	public void run() {
		try {
			ModelFactory f = (ModelFactory) ModelRegistry.getRegistry()
					.getFactory(this.goal.getModel());
			Description description = (Description) f.createElement(
					ModelPackage.__REQUIRE_DESCRIPTION__, true);
			description.setSummary("end description");
			GoalDescriptionDialog dialog = new GoalDescriptionDialog();
			dialog.setDeleteable(false);
			dialog.setDescription(description);
			int result = dialog.open();
			if (result == GoalDescriptionDialog.OK) {
				goal.getEndDescription().add(description);
			} else {

			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (FactoryNotFoundException e) {
			e.printStackTrace();
		}
	}
}
